<template>
  <div class="list" ref="wrapper">
    <div class="content">
      <!-- 当前排序城市 -->
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <button class="button">{{this.city}}</button>
          </div>
        </div>
      </div>

      <!-- 热门排序城市 -->
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <ul class="button-list" >
          <li class="button-wrapper" v-for="item of hotCities" :key="item.id" @click="handleCityClick(item.name)">
            <button class="button">{{item.name}}</button>
          </li>
        </ul>
      </div>

      <!-- 字母排序城市 -->
      <div class="area" :ref="key" v-for="(item,key) in cities" :key="key">
        <div class="title border-topbottom" >{{key}}</div>
        <ul class="item-list">
          <li class="item border-bottom" v-for="city of item" :key="city.id" @click="handleCityClick(city.name)">{{city.name}}</li>
        </ul>
      </div>

    </div>
  </div>
</template>

<script>
import BScroll from '@better-scroll/core'
import {mapState} from 'vuex'

export default {
  name: 'CityList',
  props: {
    cities: Object,
    hotCities: Array,
    letter: String
  },
  mounted () {
    this.scroll = new BScroll(this.$refs.wrapper)
  },
  computed: {
    ...mapState(['city'])
  },
  watch: {
    letter () {
      if (this.letter) {
        var element = this.$refs[this.letter][0]
        this.scroll.refresh()
        this.scroll.scrollToElement(element)
      }
    }
  },
  methods: {
    handleCityClick (city) {
      this.$store.commit('changeCity', city)
      this.$router.push('/')
    }
  }
}
</script>

<style lang="scss" scoped>
@import "../../../assets/styles/variable.scss";

.list {
  position: absolute;
  top: 1.58rem;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
  .content{
    height: 745rem;
  }
}
.border-topbottom {
  &:before {
    border-color: #ccc;
  }
  &:after {
    border-color: #ccc;
  }
}
.border-bottom {
  &:before {
    border-color: #ccc;
  }
}
.title {
  height: 0.54rem;
  line-height: 0.54rem;
  padding-left: 0.2rem;
  background-color: #eee;
  color: #666;
  font-size: 0.26rem;
}
.button-list {
  overflow: hidden;
  padding: 0.1rem 0.6rem 0.1rem 0.1rem;
  .button-wrapper {
    float: left;
    width: 33.33%;
    .button {
      width: 90%;
      margin: 0.1rem;
      padding: 0.1rem 0;
      text-align: center;
      border: 0.02rem solid #ccc;
      border-radius: 0.06rem;
      background-color: #fff;
    }
  }
}
.item-list {
  .item {
    line-height: 0.76rem;
    padding-left: 0.2rem;
  }
}
</style>
